//
//  DataAnalyzingViewController.m
//  TotalDemo
//
//  Created by gujian on 16/3/3.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "DataAnalyzingViewController.h"
#import "PieChartView.h"
#import "TDViewUtils.h"
#import "GQJProgressView.h"
#import "GQJCircleView.h"
#import "GQJPatternView.h"
#import "GQJStarView.h"

@interface DataAnalyzingViewController()

@property (nonatomic, strong) PieChartView *chartView;

@property (nonatomic, strong) GQJCircleView *circleView;

@end

@implementation DataAnalyzingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"数据可视化";

    _chartView = [[PieChartView alloc] initWithFrame:CGRectMake(50, 50, SCREEN_WIDTH-100, 100)];
//    [_chartView setProgress:0.25];
    [self.view addSubview:_chartView];
    
    
    UIButton *btn = [TDViewUtils createCustomButton];
    btn.frame = CGRectMake(0, 0, 100, 30);
    [btn setTitle:@"增加" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    GQJProgressView *progressView = [[GQJProgressView alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    [self.view addSubview:progressView];
    [progressView updateWithProgress:0.1];
    
    
    GQJCircleView *circleView = [[GQJCircleView alloc] initWithFrame:CGRectMake(100, 320, 100, 100)];
    [self.view addSubview:circleView];
    _circleView = circleView;
    
    //获取私有变量
    __unused CGFloat xProgress = [[progressView valueForKey:@"xProgress"] floatValue];
    __unused CGFloat xProgress1 = [[progressView valueForKeyPath:@"xProgress"] floatValue];
    UIView *targetValue = [[UIView alloc] init];
    NSError *error;
    __unused BOOL flag = [progressView validateValue:&targetValue forKey:@"xProgress" error:&error];
    
    GQJPatternView *patternView = [[GQJPatternView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
    [self.view addSubview:patternView];
    
    GQJStarView *starView = [[GQJStarView alloc] initWithFrame:CGRectMake(0, 100, 50, 50)];
    [self.view addSubview:starView];
    
}


- (void)buttonClickedAction:(id)sender
{
    [_chartView setProgress:0.5];
    [self testBlock];

}

/**
 *  block有三种类型:
 *  1._NSConcreateStackBlock--__NSStackBlock        存储在栈上；
 *  2._NSConcreateGlobalBlock--__NSGlobalBlock      存储在程序的数据区域（text段）；
 *  3._NSConcreateMallocBlock--__NSMallocBlock      存储在堆上
 *
 *  NSGlobalBlock:在block内部没有引用任何外部变量
 *  NSStackBlock:在block内部有引用外部变量
 *  NSMallocBlock:如果NSStackBlock需要在其作用域外部使用的时候，在MRC模式下需手动将其copy到堆上，NSMalloc支持retain、release,对其引用计数+1或-1，copy不会生成新的对象，只是增加了一次引用，类似retain;
    而ARC模式下回自动对齐进行copy，不需要自己手动管理。
    ARC模式下，id和对象类型在灭有明确指定所有权修饰符时，默认为__strong修饰符（详见Objective高级编程p30）
 
    http://www.jianshu.com/p/61ff80d310f4
 
 */
- (void)testBlock
{
//    NSInteger num = 100;
//    BOOL flag = YES;
//
//    
//    void (^block)();
//    if (flag) {
//        block = ^{
//            NSLog(@"blockA:%@",@(num));
//        };
//    }else
//    {
//        block = ^{
//            NSLog(@"blockB");
//        };
//    }
//    
//
//    
//    block();
//
//    NSLog(@"malloc blockA:%@",block);
//    
//    NSLog(@"global blockB:%@",block);
//    
//    
//    
//    NSLog(@"stack Block:%@",^{NSLog(@"stackBlock:%@",@(num));});
    
////    MRC下block分析
//    [self example];
    
}

////MRC下分析
//- (void)example
//{
//    NSMutableArray *array = [NSMutableArray array];
//    [self example_addBlocktoArrar:array];
//    void(^block)() = array[0];
//    block();
//}
//
///**
// *  在MRC下会有发生崩溃的机会
// *  因为此时的block分配在栈上，超出作用域后，所指向的内存被回收，
// *  如果此块内存被覆写，则会发生崩溃现象
// *  修改方法，对block执行copy操作，增加retainCount
// *  [array addObject:[^{NSLog(@"%cn",b)} copy]];
// */
//- (void)example_addBlocktoArrar:(NSMutableArray *)array
//{
//    char b = 'B';
//    [array addObject:^{
//        NSLog(@"%cn",b);
//    }];
//}


@end
